jQuery(function ($) {
    $('#totalGroupClientPie,#totalGroupStatusPie').css('margin-top',-266 + 'px');
    $('#totalGroupTypePieLine').css('margin-top',-552 + 'px');
    initSelectYear(4);
    $('#thisLastSelect').change(function () {
        initThisLastIssueRobTotal();
    });
    $('#totalGroupTypeSelect').change(function () {
        initTotalGroupType();
    });
    $('#groupWeekSelect').change(function () {
        initTotalGroupWeek();
    });
    $('#groupClientSelect').change(function () {
        initTotalGroupClient();
    });
    $('#issueRobDaySelect').change(function () {
        initMonthIssueRobDay();
    });
    initThisLastIssueRobTotal();
    initTotalGroupType();
    initTotalGroupWeek();
    initTotalGroupClient();
    initTotalGroupStatus();
    initTotalGroupTypePieLine();
    initMonthIssueRobDay();
});

/**
 * 初始化今年与去年每个月发布/抢的订单总量对比
 */
function initThisLastIssueRobTotal(){
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listThisLastIssueRobTotal?thisYear=' + $('#thisLastSelect').val(),
        dataType:'json',
        cache: true,
        success: function(thisLastTotalData){
            drawThisLastIssueRobTotal(thisLastTotalData);
        }
    });
}

/**
 * 画今年与去年订单总量折线图
 * @param thisLastData 今年与去年订单总量数据
 */
function drawThisLastIssueRobTotal(issueRobData) {
    let irYearArr = [],monthArr = [], issueRobArr = [];
    for (let i = 0; i < issueRobData.length; i ++){
        let irObj = issueRobData[i],irStr = '抢', monthDataArr = [];
        if (irObj.issueOrRobFlag == 'i'){
            irStr = '下';
        }
        let lineName = irObj.issueOrRobYear + irStr + '单';
        irYearArr.push(lineName);
        for (let m in irObj){
            let isMonth = m != 'issueOrRobFlag' && m != 'issueOrRobYear';
            if (isMonth){
                monthDataArr.push(irObj[m]);
            }
            //添加月份
            if (i == 0 && isMonth){
                monthArr.push(m + '月');
            }
        }
        let seriesObj = {
            name: lineName,
            type: 'line',
            data: monthDataArr
        }
        issueRobArr.push(seriesObj);
    }
    drawStackedLineChart('本/上年下/抢单总量',irYearArr,monthArr,issueRobArr,'thisLastIssueRobTotal',30,'单');
}

/**
 * 初始化订单类型总量
 */
function initTotalGroupType() {
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listTotalGroupType?thisYear=' + $('#totalGroupTypeSelect').val(),
        dataType:'json',
        cache: true,
        success: function(typeData){
            drawCommonPie('各订单类型总量及占比','类型总量(占比)',typeData,'totalGroupTypePie');
        }
    });
}
/**
 * 初始化每周几的订单总量
 */
function initTotalGroupWeek() {
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listTotalGroupWeek?thisYear=' + $('#groupWeekSelect').val(),
        dataType:'json',
        cache: true,
        success: function(weekData){
            drawTotalGroupWeek(weekData);
        }
    });
}

/**
 * 画每周几的订单总量柱状图
 * @param weekData 订单数据
 */
function drawTotalGroupWeek(weekData) {
    let xAxisArr = [],seriesArr = [], weekArr = [],numWeekArr = [1,2,3,4,5,6,7];
    for (let i = 0; i < weekData.length; i ++){
        let wd = weekData[i];
        weekArr.push(wd.orderWeek);
        xAxisArr.push(getDayWeek(wd.orderWeek));
        seriesArr.push(wd.orderTotal);
    }
    //如果weekData中没有某个周几的数据，则在此处加进去
    for (let g = 0; g < numWeekArr.length; g ++){
        let num = numWeekArr[g];
        if (weekArr.indexOf(num) >= 0){
            continue;
        }
        //添加一个元素
        xAxisArr.splice(num - 1,0,getDayWeek(num));
        seriesArr.splice(num - 1,0,0);
    }
    let weekOption = getSimpleBarOption('周几的总订单量',xAxisArr,seriesArr,'单');
    setChartOption(weekOption,'totalGroupWeekBar');
}
/**
 * 初始化订单类型总量
 */
function initTotalGroupClient() {
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listTotalGroupClient?thisYear=' + $('#groupClientSelect').val(),
        dataType:'json',
        cache: true,
        success: function(clientData){
            drawCommonPie('订单来源客户端总量及占比','客户端总量(占比)',clientData,'totalGroupClientPie');
        }
    });
}
/**
 * 初始化订单类型总量
 */
function initTotalGroupStatus() {
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listStatusTotal?thisYear=' + $('#groupClientSelect').val(),
        dataType:'json',
        cache: true,
        success: function(statusData){
            for (let i = 0; i < statusData.length; i ++){
                statusData[i].name = getStatusByCode(statusData[i].name);
            }
            drawCommonPie('各订单状态总量及占比','状态总量(占比)',statusData,'totalGroupStatusPie');
        }
    });
}
function initTotalGroupTypePieLine() {
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listMonthOrderTypeTotal',
        dataType:'json',
        cache: true,
        success: function(typeData){
            drawGroupTypePieLine(typeData);
        }
    });
}

/**
 * 画今年各类型订单总量的饼状图和折线图
 * @param typeData 今年各类型订单总量数据
 */
function drawGroupTypePieLine(typeData) {
    // 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(I('totalGroupTypePieLine'));
    let sourceArr = [['product', '1', '2', '3', '4', '5', '6','7', '8', '9', '10', '11', '12']],seriesArr = [];
    for(let g = 0; g < typeData.length; g ++){
        seriesArr.push({
            type: 'line',
            smooth: true,
            seriesLayoutBy: 'row'
        });
        let dataArr = [], toObj = typeData[g];
        for (let v in toObj){
            if (v != 'typeName'){
                dataArr.push(toObj[v]);
            }
        }
        dataArr.splice(0,0,toObj.typeName);
        sourceArr.push(dataArr);
    }
    seriesArr.push({
        type: 'pie',
        id: 'pie',
        radius: '30%',
        center: ['66%', '20%'],
        label: {
            formatter: '{b}: {@1} ({d}%)'
        },
        encode: {
            itemName: 'product',
            value: '1',
            tooltip: '1'
        }
    });
    setTimeout(function() {
        let option = {
            title: {
                text: '每个月各类型订单数',
                top: '276px'
            },
            legend: {
                left: '36%',
                top: '236px'
            },
            tooltip: {
                trigger: 'axis',
                showContent: false
            },
            dataset: {
                source: sourceArr
            },
            xAxis: {
                type: 'category',
                boundaryGap: false
            },
            yAxis: {
                gridIndex: 0,
                name: '个',
                nameTextStyle:{
                    align: 'right'
                }
            },
            grid: {
                top: '348px',
                left: '1%',
                right: '2%',
                bottom: '1%',
                containLabel: true
            },
            series: seriesArr
        };

        myChart.on('updateAxisPointer', function(event) {
            let xAxisInfo = event.axesInfo[0];
            if(!xAxisInfo) {
                return;
            }
            let dimension = xAxisInfo.value + 1;
            myChart.setOption({
                series: {
                    id: 'pie',
                    label: {
                        formatter: '{b}: {@[' + dimension + ']} ({d}%)'
                    },
                    encode: {
                        value: dimension,
                        tooltip: dimension
                    }
                }
            });
        });
        myChart.setOption(option);
    });
}
/**
 * 初始化每个月从下单到抢单、抢单到完成订单所花的平均天数
 */
function initMonthIssueRobDay(){
    $.ajax({
        type: "POST",
        url: '/orderOtherChart/listMonthIssueRobDay?thisYear=' + $('#issueRobDaySelect').val(),
        dataType:'json',
        cache: true,
        success: function(dayData){
            drawMonthIssueRobDayLine(dayData);
        }
    });
}

/**
 * 画今每个月从下单到抢单、抢单到完成订单所花的平均天数折线图
 * @param thisLastData 数据集合
 */
function drawMonthIssueRobDayLine(dayData) {
    let irdLegendArr = ['下单到抢单','抢单到完成'],monthArr = [], irdSeriesArr = [], iTorData = [], rTofData = [];
    for (let g = 0; g < dayData.length; g ++){
        let muObj = dayData[g];
        let lineName = muObj.myMonth + '月';
        monthArr.push(lineName);
        iTorData.push(muObj.issueToRobDay);
        rTofData.push(muObj.robToFinishDay);
    }
    for (let g = 0; g < irdLegendArr.length; g ++){
        irdSeriesArr.push({
            name: irdLegendArr[g],
            type: 'line',
            data: g == 0 ? iTorData : rTofData
        });
    }
    drawStackedLineChart('  下单到抢单、抢单到完成平均天数',irdLegendArr,monthArr,irdSeriesArr,'monthIssueRobDayLine',50,'天');
}